<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

namespace Al\Style\Lib\Menu;

use Al\Core\Lib\Service,
    Al\HTTP\Lib\Route;

/**
 * Represents an html menu link.
 *
 * @author Eugene Kuzmenko <mail@aljey.com>
 */
class Link extends Item {

  /**
   * @var Route A route object.
   */
  protected $route;

  /**
   * @var array A collection of array parameters.
   */
  protected $params;

  /**
   * @var Menu A menu instance.
   */
  protected $menu;

  /**
   * @var string A text to show inside of a link.
   */
  protected $title;

  /**
   * @param array $config [optional]
   */
  public function __construct($config = null) {
    if (!$config) $config = array();
    if (!is_array($config)) $config = array($config);
    $this->route = Service::get('config.routes.' . (isset($config['route']) ?
      $config['route'] : ''));
    $this->params = isset($config['params']) ? $config['params'] : array();
    $this->menu = (isset($config['menu']) && count($config['menu'])) ?
      new Menu($config['menu']) : '';
    if ($this->menu) $this->menu->class = 'dropdown-menu';
    $this->title = isset($config['title']) ? $config['title'] : '';
  }

  /**
   * @return string
   */
  public function getTitle() {
    return $this->title;
  }

  /**
   * @return Menu
   */
  public function getMenu() {
    return $this->menu;
  }

  /**
   * @return string
   */
  public function getUrl() {
    return $this->route ? $this->route->buildPath($this->params) : '#';
  }

  /**
   * Checks if the link is active (it's menu is active or its route and params
   * match the ones in the current master request).
   *
   * @return bool
   */
  public function isActive() {
    return ($this->menu && $this->menu->isActive()) || $this->match();
  }

  /**
   * Checks if the route object and params match the ones in the current master
   * request.
   *
   * @return bool
   */
  public function match() {
    $http = Service::get('http');
    $params = $http['params']->getMaster();
    $query = $http['query'];
    if (!isset($params['route']) || !$this->route
      || $params['route'] !== $this->route) return false;
    foreach ($this->params as $key => $value) {
      if ((!isset($params[$key]) || $params[$key] != $value) &&
        (!isset($query[$key]) || $query[$key] != $value)) return false;
    }
    return true;
  }

  /**
   * @return string
   */
  public function __toString() {
    $attr = '';
    $caret = '';
    $cls = array();
    if ($this->menu) {
      $cls[] = 'dropdown';
      if ($this->isNav) {
        $attr = ' data-toggle="dropdown"';
        $caret = ' <b class="caret"></b>';
      }
    }
    if ($this->isActive()) $cls[] = 'active';
    $cls = count($cls) ? (' class="' . implode(' ', $cls) . '"') : '';
    return "<li$cls><a href=\"" . $this->getUrl() .
      "\"$attr>$this->title$caret</a>$this->menu</li>";
  }

} 